from time import sleep
from selenium import webdriver
from HTMLTestRunner import HTMLTestRunner
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.wait import WebDriverWait
import unittest


class TestJXCSys(unittest.TestCase):
    driver = webdriver.Firefox()

    @classmethod
    def setUpClass(cls):
        cls.driver.implicitly_wait(2)
        cls.driver.get('http://localhost/myweb/jxc/index.asp')

    @classmethod
    def tearDownClass(cls):
        cls.driver.quit()

    def setUp(self):
        sleep(1)

    def tearDown(self):
        sleep(1)

    def test_login(self):
        self.driver.find_element_by_name('username').send_keys('admin')
        self.driver.find_element_by_name('pwd').send_keys('admin')
        self.driver.find_element_by_name('enter').click()
        self.assertIn('后台管理系统', self.driver.page_source)

    def test_exit(self):
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame('topFrame')
        self.driver.find_element_by_link_text('安全退出').click()
        sleep(1)
        alt = ec.alert_is_present()(self.driver)
        if alt:
            alt.accept()
        sleep(3)
        # 由于框架原因，必须重新切换，否则提示：“TypeError: can't access dead object”
        self.driver.switch_to.default_content()
        self.assertIn('系统登陆', self.driver.page_source)

    def test_del_unit(self):
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame('left')
        module_xpath = '/html/body/table/tbody/tr[13]'
        menu_xpath = '/html/body/table/tbody/tr[14]/td/table/tbody/tr[7]/td'
        self.driver.find_element_by_xpath(module_xpath).click()
        self.driver.find_element_by_xpath(menu_xpath).click()
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame('right')
        sleep(1)

        self.driver.find_element_by_id('chkall').click()
        bt_del_css_path = 'input.button:nth-child(2)'
        self.driver.find_element_by_css_selector(bt_del_css_path).click()
        self.driver.switch_to.alert.accept()
        sleep(1)
        self.assertNotIn("name='ID'", self.driver.page_source)

    def test_add_unit(self):
        bt_css_path = 'input.button:nth-child(1)'
        wait = WebDriverWait(self.driver, 5)
        bt_add = wait.until(ec.presence_of_element_located((By.CSS_SELECTOR, bt_css_path)))
        bt_add.click()

        self.driver.find_element_by_name('danwei').send_keys('New Unit')
        self.driver.find_element_by_name('submit').click()
        self.driver.switch_to.alert.accept()
        sleep(1)
        self.assertIn('New Unit', self.driver.page_source)


def test_suite():
    suite = unittest.TestSuite()
    suite.addTest(TestJXCSys('test_login'))
    suite.addTest(TestJXCSys('test_del_unit'))
    suite.addTest(TestJXCSys('test_add_unit'))
    suite.addTest(TestJXCSys('test_exit'))
    return suite


with open('d:/r.html', 'wb') as f:
    runner = HTMLTestRunner(stream=f, title='进销存测试报告')
    runner.run(test_suite())
